<?php

include_once 'DBCon.php';
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of RouteStation
 *
 * @author user
 */
class RouteStation {

    private $rid;
    private $routename;
    private $stationname;
    private $location;
    private $sid;
    private $travelTime;
    private $fare;
    private $con;
    private $selectroute = 'SELECT rid, routeName FROM route WHERE status = "ACV";';
    private $selectorigin = 'SELECT s.sid, s.stationName FROM station s, stationinroute sr WHERE rid = ? AND sr.sid = s.sid AND status = "ACV";';
    private $selectascdestination = 'SELECT s.sid, s.stationName FROM station s, stationinroute sr WHERE rid = ? AND sr.sid > ? AND sr.sid = s.sid AND status = "ACV" ORDER BY s.sid ASC;';
    private $selectdecdestination = 'SELECT s.sid, s.stationName FROM station s, stationinroute sr WHERE rid = ? AND sr.sid < ? AND sr.sid = s.sid AND status = "ACV" ORDER BY s.sid DESC;';
    private $selectdestination = 'CALL getDestinationStation(?, ?);';

    function __construct() {
        $this->con = DBCon::connectDB();
    }

    public function getRoute() {
        $result = array();
        $data = null;
        $pstmtselect = $this->con->prepare($this->selectroute);
        $pstmtselect->bind_result($result['rid'], $result['routeName']);
        $pstmtselect->execute();
        while ($pstmtselect->fetch()) {
            foreach ($result as $key => $value) {
                $row[$key] = $value;
            }
            $data[] = $row;
        }
        if ($data != null) {
            return $data;
        } else {
            return null;
        }
    }

    public function getOrigin() {
        $result = array();
        $data = null;
        $rid = trim($_POST['rid']);
        $pstmtselect = $this->con->prepare($this->selectorigin);
        $pstmtselect->bind_param('s', $rid);
        $pstmtselect->bind_result($result['sid'], $result['stationName']);
        $pstmtselect->execute();
        while ($pstmtselect->fetch()) {
            foreach ($result as $key => $value) {
                $row[$key] = $value;
            }
            $data[] = $row;
        }
        if ($data != null) {
            return $data;
        } else {
            return null;
        }
    }

    public function getDestination() {
        $data = null;
        $rid = trim($_POST['rid']);
        $sid = trim($_POST['sid']);
        $rs = $this->con->query('CALL getDestinationStation(' . $rid . ', ' . $sid . ');');
        while($row = $rs->fetch_assoc()){
            $data[] = $row;
        }
        if ($data != null) {
            return $data;
        } else {
            return null;
        }
    }

}

?>
